/**
 * Sistem Informasi Penjualan
 * @author Kelompok 1
 * Ligar Sekar Wangi
 * Tri Maryani
 * Winda Rahadyan
 * TIF/IV/A
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class Barang extends JInternalFrame implements ActionListener, FocusListener {
	private JPanel 		jpBarang1 		= new JPanel();
	private JLabel 		lblKode_Barang	= new JLabel ("Kode Barang :"),
						lblNama_Barang	= new JLabel ("Nama Barang :"), 
						lblHarga_Beli	= new JLabel ("Harga Beli  :"), 
						lblHarga_Jual   = new JLabel ("Harga Jual  :"),
						lblQuantity     = new JLabel ("Quantity    :");
	private JTextField 	txtKode_Barang	= new JTextField (), 
						txtNama_Barang	= new JTextField (), 
						txtHarga_Beli	= new JTextField (),
						txtHarga_Jual   = new JTextField (),
						txtQuantity     = new JTextField ();
	private JButton 	btnAdd  		= new JButton (), 
						btnEdit			= new JButton (), 
						btnDelete		= new JButton (), 
						btnKosong		= new JButton ();	
	
	Barang () {
		/* super(Judul, UkuranFrame, TombolClose, TombolMaksimum, TombolMinimum)*/
		super ("Form Barang", false, true, false, true);
		setSize (470, 250);
		
		jpBarang1.setLayout (null);

		/* Mengatur letak objek Label Di container*/
		lblKode_Barang.setBounds (15, 20, 100, 25);
	    lblNama_Barang.setBounds (15, 55, 100, 25);
		lblHarga_Beli.setBounds (15, 90, 100, 25);
		lblHarga_Jual.setBounds (15, 125, 100, 25);
        lblQuantity.setBounds (15, 160, 100, 25);
        
		/* Mengatur letak objek Text Di Container */
		txtKode_Barang.setBounds (115, 20, 100, 25);
		txtNama_Barang.setBounds (115, 55, 205, 25);
		txtHarga_Beli.setBounds (115, 90, 92, 25);
		txtHarga_Jual.setBounds (115, 125, 100, 25);
		txtQuantity.setBounds (115, 160,100,25);	

		/* Mengatur letak objek Button di Container */
		btnAdd.setBounds (340, 20, 85, 25);	
		btnAdd.setLabel("Add");	
		btnEdit.setBounds (340, 55, 85, 25);
		btnEdit.setLabel("Edit");
		btnDelete.setBounds (340, 90, 85, 25);
		btnDelete.setLabel("Delete");
		btnKosong.setBounds (340, 125, 85, 25);
		btnKosong.setLabel("Bersih");

		/* Mengatur Perataan teks di Text */ 
		txtHarga_Beli.setHorizontalAlignment (JTextField.RIGHT);
        txtHarga_Jual.setHorizontalAlignment (JTextField.RIGHT);
        txtQuantity.setHorizontalAlignment (JTextField.RIGHT);
        
		/* Objek Button di Non Aktifkan dan di aktifkan */
		btnAdd.setEnabled(false);		
		btnEdit.setEnabled(false);			
		btnDelete.setEnabled(false);			
		btnKosong.setEnabled(true);			

		/* Mengatur objek untuk dapat berinteraksi */
		txtKode_Barang.addFocusListener (this);
		btnAdd.addActionListener (this);
		btnEdit.addActionListener (this);
		btnDelete.addActionListener (this);
		btnKosong.addActionListener (this);

		/* Text Harga_Beli di input hanya bilangan saja pada textfield */
		txtHarga_Beli.addKeyListener (new KeyAdapter() 
			{
			public void keyTyped (KeyEvent ke) 
				{
				char c = ke.getKeyChar ();
				if (!((Character.isDigit (c) || (c == KeyEvent.VK_BACK_SPACE)))) 
					{
					getToolkit().beep (); ke.consume ();
      				}
    			}
    		});

        /* Text Harga_Jual di input hanya bilangan saja pada textfield */
		txtHarga_Jual.addKeyListener (new KeyAdapter() 
			{
			public void keyTyped (KeyEvent ke) 
				{
				char c = ke.getKeyChar ();
				if (!((Character.isDigit (c) || (c == KeyEvent.VK_BACK_SPACE)))) 
					{
					getToolkit().beep (); ke.consume ();
      				}
    			}
    		});
    		
		// Meletakkan seluruh kontrol pada objek panel */
		jpBarang1.add (lblKode_Barang);	
		jpBarang1.add (txtKode_Barang);
		jpBarang1.add (lblNama_Barang);	
		jpBarang1.add (txtNama_Barang);
		jpBarang1.add (lblHarga_Beli);		
		jpBarang1.add (txtHarga_Beli); 
		jpBarang1.add (lblHarga_Jual);		
		jpBarang1.add (txtHarga_Jual);
		jpBarang1.add (lblQuantity);		
		jpBarang1.add (txtQuantity);
		jpBarang1.add (btnAdd);		
		jpBarang1.add (btnEdit); 
		jpBarang1.add (btnDelete);	
		jpBarang1.add (btnKosong);

		/* Menambahkan objek panel (jpBarang1) ke container frame */
		getContentPane().add (jpBarang1);
		
		/* Menampilkan frame ke layar monitor */
		setVisible (true);
	}

    /* Fungsi untuk memeriksa kursor saat meninggalkan objek txtKode_Barang */
	public void focusGained (FocusEvent fe) { }
	public void focusLost (FocusEvent fe) {
		if (txtKode_Barang.getText().equals ("")) { }
		else {
			Cari();
		}
	}

	/* Fungsi jika user melakukan action penekanan tombol Button */
	public void actionPerformed (ActionEvent ae) {
		Object obj = ae.getSource();
		if (obj == btnAdd) 	
			{ 
			 Add(); 
			}				
		if (obj == btnEdit) 	
			{ 
			 Edit(); 	
			}
		if (obj == btnDelete) 	
			{ 
			 Delete(); 	
			}
		Kosong();
	}
	    
	/* Fungsi untuk mencari Kode_Barang ke tabel barang */
	void Cari(){
		try	{
			Connect ObjKoneksi1 = new Connect();
	   		Connection con1 = ObjKoneksi1.OpenConnect();
	   		Statement st1 = con1.createStatement();
	   		String sql1 = "SELECT * FROM Barang WHERE Kode_Barang ='" + txtKode_Barang.getText () + "'";
	   		ResultSet rs1 = st1.executeQuery(sql1);
			if(rs1.next())	
				{
				/* Jika Kode Barang Ditemukan Di Tabel Barang */
				txtKode_Barang.setText (rs1.getString ("Kode_Barang"));
				txtNama_Barang.setText (rs1.getString ("Nama_Barang"));
				txtHarga_Beli.setText (rs1.getString ("Harga_Beli"));
				txtHarga_Jual.setText (rs1.getString ("Harga_Jual"));
				txtQuantity.setText (rs1.getString ("Quantity"));
				btnAdd.setEnabled(false);
				btnEdit.setEnabled(true);
				btnDelete.setEnabled(true);
				txtNama_Barang.requestFocus ();
			    }
			else 
				{
				/* Jika Kode Barang Tidak ketemu di Tabel Barang */
				btnAdd.setEnabled(true);
				btnEdit.setEnabled(false);
				btnDelete.setEnabled(false);
				txtNama_Barang.requestFocus ();
			}
   			rs1.close();
   			con1.close();
		}
		catch(SQLException e) 
		{
		}
	}

	// Fungsi untuk menambahkan data ke tabel barang
	void Add()
		{
		try	
			{
	   		Connect ObjKoneksi1 = new Connect();
	   		Connection con1 = ObjKoneksi1.OpenConnect();
	   		Statement st1 = con1.createStatement();
	   		String sql1 = "insert into Barang(Kode_Barang,Nama_Barang,Harga_Beli,Harga_Jual,Quantity) values('"+txtKode_Barang.getText()+
	   			"','"+txtNama_Barang.getText()+
	   			"','"+txtHarga_Beli.getText()+
	   			"','"+txtHarga_Jual.getText()+
	   			"','"+txtQuantity.getText()+"')";
	   		int rows1 = st1.executeUpdate(sql1);
 			if (rows1 == 1)
  	    		JOptionPane.showMessageDialog(this, "Sukses Di Tambahkan!!!");
 			con1.close();
		    }
		catch(SQLException e) 
		    {
		    }
	    }
		
	// Fungsi untuk mengedit Data di Tabel Barang */
	void Edit()
		{
		try	
			{
	   		 Connect ObjKoneksi1 = new Connect();
	   		 Connection con1 = ObjKoneksi1.OpenConnect();
	   		 Statement st1 = con1.createStatement();
	   		 String sql1 = "update Barang set Nama_Barang='"+txtNama_Barang.getText()+
	   		 	   "',Harga_Beli='"+ txtHarga_Beli.getText()+
	   		 	   "',Harga_Jual='"+txtHarga_Jual.getText()+
	   		 	   "',Quantity='"+txtQuantity.getText()+
	   		 	   "' where Kode_Barang='"+txtKode_Barang.getText()+"' ";		   		
			int rows1 = st1.executeUpdate(sql1);
 			if (rows1 == 1)
  	    		JOptionPane.showMessageDialog(this, "Sukses Di Edit!!!");
  			con1.close();
		    }
		catch(SQLException e) 
			{
		    }
	   }

	// Fungsi untuk menghapus Rekord Di Tabel Barang */
	void Delete()
		{
		try	
			{
	   		 Connect ObjKoneksi1 = new Connect();
	   		 Connection con1 = ObjKoneksi1.OpenConnect();
	   		 Statement st1 = con1.createStatement();
	   		 String sql1 = "delete from Barang where Kode_Barang='"+txtKode_Barang.getText()+"' ";		   		
			 int rows1 = st1.executeUpdate(sql1);
 			 if (rows1 == 1)
  	    		JOptionPane.showMessageDialog(this, "Sukses Di Hapus!!!");
 			    con1.close();
	    	}
		catch(SQLException e) 
			{
		    }
	    }

	/* Fungsi untuk mengkosongkan Objek masukan */
	void Kosong () 
		{
		 txtKode_Barang.setText ("");
		 txtNama_Barang.setText ("");
		 txtHarga_Beli.setText ("");
		 txtHarga_Jual.setText ("");
		 txtQuantity.setText ("");
		 btnAdd.setEnabled(false);
		 btnEdit.setEnabled(false);
		 btnDelete.setEnabled(false);
		 txtKode_Barang.requestFocus ();
	    }
}	